package jlisp.ui.composite;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

import jlisp.tools.SettingTools;
import org.eclipse.wb.swt.SWTResourceManager;

import com.moro.lisp.JLisp;
import com.moro.lisp.JLispData;
import com.moro.lisp.JLispType;
import com.moro.lisp.os.Terminal;


import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class MainComposite extends Composite {
	private Text text;
	JLisp lisp;
	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public MainComposite(Composite parent, MessageBox messageBox, int style, Display display) {
		super(parent, style);
		//setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		this.setBounds(0, 0, 1062, 465);
		SettingTools.setControlSelfAdapt(this);
		
		Label lblNewLabel = new Label(this, SWT.CENTER);
		//lblNewLabel.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		//lblNewLabel.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		lblNewLabel.setBounds(200, 0, 662, 20);
		lblNewLabel.setText("JLisp.v1");
		
		text = new Text(this, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
//		text.setBounds(10, 44, 902, 300);
		text.setBounds(10, 44, 902, 300);
		//text.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLACK));
		//text.setForeground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
		text.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDoubleClick(MouseEvent e) {
				text.selectAll();
			}
		});
		
		ConsoleComposite consoleComposite = new ConsoleComposite(this, SWT.NONE);
		SettingTools.setVisibleFalse(consoleComposite);
		
		lisp = new JLisp();
		
		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.setBounds(918, 79, 134, 30);
		btnNewButton.setText("Run");
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			//13sss10
			@Override
			public void widgetSelected(SelectionEvent e) {
				String context = text.getText();
				context = context.replace("；", ";");
				String[] exprs = context.split(";");
				JLispData ans = null;
				for(String expr: exprs) {
					if("".equals(expr)) {
						continue;
					}
					String exp = toRightExpr(expr);
					if("".equals(exp)) {
						continue;
					}
					ans = lisp.lisp(exp);
					
					
					if(JLispType.ERROR == ans.type) {
						break;
					}
				}
				
				lisp.closeDebugMode();
				
				if(null == ans) {
					return;
				}
				SettingTools.setVisibleTrue(consoleComposite);
				consoleComposite.setLocation(10, 350);
				if(JLispType.ERROR == ans.type) {
					
					Terminal.clearMessage();
					consoleComposite.addMessage(Terminal.getStringFromData(ans), SWTResourceManager.getColor(SWT.COLOR_RED));
				}else {
					for(String message:Terminal.messages) {
						consoleComposite.addMessage(message, SWTResourceManager.getColor(SWT.COLOR_YELLOW));
					}
					Terminal.clearMessage();
					consoleComposite.addMessage(">> " + Terminal.getStringFromData(ans), SWTResourceManager.getColor(SWT.COLOR_WHITE));
				}
				
			}
		});
		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				lisp = new JLisp();
				messageBox.setMessage("JLisp engine has been refreshed");
				messageBox.open();
			}
		});
		btnNewButton_1.setBounds(918, 259, 134, 30);
		btnNewButton_1.setText("Flush Cache");
		
		
		Button btnNewButton_2 = new Button(this, SWT.NONE);
		btnNewButton_2.setBounds(918, 322, 15, 22);
		btnNewButton_2.setText("x");
		btnNewButton_2.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				consoleComposite.clearMessage();
				SettingTools.setVisibleFalse(consoleComposite);
			}
		});
		
		
		Button btnNewButton_3 = new Button(this, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(lisp.isLogMode()) {
					lisp.lisp("(debug 'closelog)");
				}else {
					lisp.lisp("(debug 'log)");
				}
			}
		});
		btnNewButton_3.setBounds(918, 169, 134, 30);
		btnNewButton_3.setText("Log Callstack");
	}
	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}
	private String toRightExpr(String expr) {
		/*
		 * 棣栧厛灏嗚幏鍙栧埌鐨勫瓧绗︿覆涓墍鏈夋崲琛岀鐢ㄧ┖鏍兼浛浠�
		 */
		expr = expr.replace("\r\n", " ");
		expr = expr.replace("\n", " ");
		expr = expr.replace("\t", " ");
		expr = expr.replace("（", "(");
		expr = expr.replace("）", ")");
		/*
		 * 鐒跺悗灏嗗涓┖鏍肩敤涓�涓┖鏍兼浛浠�
		 */
		int spaceSize = getSerialSpaceSize(expr);
		if(spaceSize > 1) {
			for(int size = spaceSize;size > 1;size--) {
				String sizeSpace = "";
				for(int i = 0;i < size;i++) {
					sizeSpace += " ";
				}
				expr = expr.replace(sizeSpace, " ");
			}
		}
		expr = expr.replace("( )", "()");
		expr = expr.replace("( (", "((");//闇�瑕佹浛鎹袱閬�
		expr = expr.replace("( (", "((");
		expr = expr.replace(") )", "))");//闇�瑕佹浛鎹袱閬�
		expr = expr.replace(") )", "))");
		if(" ".equals(expr)) {
			return "";
		}
		return expr;	
	}
	private int getSerialSpaceSize(String expr) {
		int maxSize = 0;
		int size = 0;
		for(int i = 0;i < expr.length();i++) {
			if(expr.charAt(i) == ' ') {
				size += 1;
				if(size > maxSize) {
					maxSize = size;
				}
			}else  {
				size = 0;
			}
		}
		return maxSize;
	}
}
